﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MidiMix
{
    public partial class MarkersControl : UserControl
    {
        private int leftMargin=10; 
        private int rightMargin=10;
        private const int markerHalfHeight = 7; //
        public int LeftMargin { get { return leftMargin; } set { leftMargin = value; } }
        public int RightMargin { get { return rightMargin; } set { rightMargin = value; } }

        private double inLoc= 0D;
        private double outLoc =0D; // values between 0 and 100 (i.e. a percentage through the file)
        private SolidBrush markerBrush = new SolidBrush(Color.DarkGreen);
        private Pen markerPen = Pens.Black;

        private bool inLocSelected = false;
        private bool outLocSelected = false;
        private int selectedXOffset = 0;
        public double InLoc // value between 0 and 100 (not between 0 and 1).
        {
            get { return inLoc; }
            set { inLoc = value; }
        }
        public double OutLoc
        {
            get { return outLoc; }
            set { outLoc = value; }
        }

        public MarkersControl()
        {
            InitializeComponent();
        }
        private int markerRangeInPixels {
            get {return this.Width - leftMargin - rightMargin;}
        }
        private int markerInLoc {
            get { return leftMargin + (int)((double)markerRangeInPixels * (inLoc / 100D)); }
            set {
                inLoc = 100D * ((double)value - (double)leftMargin) / (double)markerRangeInPixels; 
            }
        }
        private int markerOutLoc {
            get { return leftMargin + (int)((double)markerRangeInPixels * (outLoc / 100D)); }
            set {
                outLoc = 100D * ((double)value - (double)leftMargin) / (double)markerRangeInPixels;
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (Enabled)
            {
                drawMarkerAt(e.Graphics, markerInLoc, false);
                drawMarkerAt(e.Graphics, markerOutLoc, true);
                e.Graphics.DrawLine(markerPen, markerInLoc, markerHalfHeight, markerOutLoc, markerHalfHeight);
            }
        }
        private void drawMarkerAt(Graphics g, int x, bool end)
        {
            Point[] pts = new Point[3];
            pts[0].X = x;
            pts[0].Y = 0;
            pts[1].X = end ? x + markerHalfHeight: x-markerHalfHeight;
            pts[1].Y = markerHalfHeight;
            pts[2].X = x;
            pts[2].Y = markerHalfHeight*2;
            g.FillPolygon(markerBrush, pts);
        }

        private void MarkersControl_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void MarkersControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.X >= markerInLoc - markerHalfHeight && e.X <= markerInLoc && e.Y >= 0 && e.Y <= 10)
            {
                inLocSelected = true;
                selectedXOffset = e.X - markerInLoc;
            }
            else if (e.X >= markerOutLoc && e.X <= markerOutLoc + markerHalfHeight && e.Y >= 0 && e.Y <= 10)
            {
                outLocSelected = true;
                selectedXOffset = e.X - markerOutLoc;
            }
        }

        private void MarkersControl_MouseUp(object sender, MouseEventArgs e)
        {
            inLocSelected= outLocSelected =false;
        }

        private void MarkersControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (inLocSelected)
            {
                markerInLoc = Math.Min(markerOutLoc, Math.Max(leftMargin, (e.X-selectedXOffset)));
            }
            else if (outLocSelected)
            {
                markerOutLoc = Math.Min(Width - rightMargin, Math.Max(markerInLoc, (e.X-selectedXOffset)));
            }
            Invalidate();
        }
    }
}
